package com.itheima.demo5owns;

import java.lang.invoke.VarHandle;
import java.util.*;

/**
 * @author 小赫
 * <p>2025/3/14 19:19
 */
public class Room {
    private List<Card> cards = new ArrayList<>();

    // 初始化,添加牌
    {
        // 1 准备点数
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 2 准备花色
        String[] colors = {"♥", "♦", "♣", "♠"};
        int num = 1;
        // 3 循环遍历，把牌添加到集合中
        for (String size : sizes) {
            for (String color : colors) {
                cards.add(new Card(color, size,++num));
            }
        }
        // 添加大小王 addAll
        Collections.addAll(cards,new Card("", "☼",++num),new Card("", "☺",++num));
        System.out.println("新牌是:"+cards);
        // 6 打乱牌
        Collections.shuffle(cards);
        // 7 输出
        System.out.println("洗牌后:"+cards);
          // 8 发牌  定义三个玩家 :小李 小王 小花
        Map<String, List<Card>> players = new HashMap<>();
        ArrayList<Card> xiaoli = new ArrayList<>(cards.subList(0, 17));
        ArrayList<Card> xiaowang = new ArrayList<>(cards.subList(17, 34));
        ArrayList<Card> xiaohua = new ArrayList<>(cards.subList(34, 51));

        // 9 排序 , 按3-10 J Q K A 2 小王 大王 的顺序排
        sortCards(xiaoli);
        sortCards(xiaowang);
        sortCards(xiaohua);

        players.put("小李", xiaoli);
        players.put("小王", xiaowang);
        players.put("小花", xiaohua);

        System.out.println("发牌");
        for (Map.Entry<String, List<Card>> entry : players.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }


        // 10 底牌排序
        System.out.println("底牌:"+cards.subList(51, 54));
        // 11 看牌



    }

    public void playGame() {
        System.out.println("游戏开始");


    }
    // 排序方法
    private void sortCards(List<Card> cards) {
        cards.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                // o1  2♠
                // o2  J♠
                return o1.getNum() - o2.getNum();
            }
        });
    }

    public List<Card> getCards() {
        return cards;
    }

}
